<?php

namespace App\Kuafu\Export\Jobs;

use App\Kuafu\Export\Model\ExportDownloadModel;
use App\Kuafu\Export\Service\ExportFactory\CreateDistributionQrcodeService;
use App\Kuafu\Export\Service\ExportFactory\CreateSourceMaterialCompressService;
use App\Kuafu\Export\Service\ExportFactory\CreateSourceMaterialTemplateService;
use App\Kuafu\Export\Service\ExportFactory\ExportAppInGoodsService;
use App\Kuafu\Export\Service\ExportFactory\ExportAppInOrderService;
use App\Kuafu\Export\Service\ExportFactory\ExportAppPayService;
use App\Kuafu\Export\Service\ExportFactory\ExportAppWmGoodsService;
use App\Kuafu\Export\Service\ExportFactory\ExportAppWmOrderService;
use App\Kuafu\Export\Service\ExportFactory\ExportMybankExpenseRecordListService;
use App\Kuafu\Export\Service\ExportFactory\ExportMybankWalletRecordService;
use App\Kuafu\Export\Service\ExportFactory\ExportPriceTagService;
use App\Kuafu\Export\Service\ExportFactory\ExportShopRecordListService;
use App\Kuafu\Export\Service\ExportFactory\ExportShopStaffRecordListService;
use App\Kuafu\Export\Service\ExportFactory\ExportWebInOrderService;
use App\Library\FeiShu\SendFs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Kuafu\File\FileUtil;

/**
 * 排队导出
 * Class OrderPayPolling
 * @package App\Kuafu\Order\Jobs
 */
class QueueExport implements ShouldQueue
{
    /**
     * php artisan queue:work redis --queue={MERCHANT-API-DELAY-CACHE}
     */
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    const QUEUE_NAME = '{QUEUE-EXPORT}';
    const QUEUE_SOURCE_MATERIAL_TEMPLATE_NAME = '{QUEUE-EXPORT-SOURCE-MATERIAL-TEMPLATE}';//物料素材模版

    public $timeout = 1200;

    private int $id;

    /**
     * Create a new job instance.
     *
     */
    public function __construct(int $id)
    {
        $this->id = $id;
        Log::info('QueueExport In:', ['id' => $id]);
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $export = ExportDownloadModel::query()->find($this->id);
        if (!$export) {
            Log::warning("QueueExport id empty: {$this->id}");
            $this->delete();
            return;
        }

        // 校验状态
        if (in_array($export->status, [ExportDownloadModel::STATUS_FINISH, ExportDownloadModel::STATUS_ERROR])) {
            Log::warning("QueueExport status fail: {$this->id} {$export->status}");

            $this->delete();
            return;
        }

        // 开始导出
        $export->dealing();

        switch ($export->fileType) {
            case ExportDownloadModel::FILE_TYPE_WEB_IN_ORDER:
                $class = ExportWebInOrderService::class;
                break;
            case ExportDownloadModel::FILE_TYPE_APP_IN_GOODS:
                $class = ExportAppInGoodsService::class;
                break;
            case ExportDownloadModel::FILE_TYPE_APP_WM_GOODS:
                $class = ExportAppWmGoodsService::class;
                break;
            case ExportDownloadModel::FILE_TYPE_APP_IN_ORDER:
                $class = ExportAppInOrderService::class;
                break;
            case ExportDownloadModel::FILE_TYPE_APP_WM_ORDER:
                $class = ExportAppWmOrderService::class;
                break;
            case ExportDownloadModel::FILE_TYPE_APP_PAY:
                $class = ExportAppPayService::class;
                break;
            case ExportDownloadModel::FILE_TYPE_APP_SOURCE_MATERIAL_TEMPLATE:
                $class = CreateSourceMaterialTemplateService::class;
                break;
            case ExportDownloadModel::FILE_TYPE_APP_DISTRIBUTION_QRCODE:
                $class = CreateDistributionQrcodeService::class;
                break;
            case ExportDownloadModel::FILE_TYPE_WEB_ACTIVITY_MATERIAL:
            case ExportDownloadModel::FILE_TYPE_WEB_GIFT_ACTIVITY_MATERIAL:
                $class = CreateSourceMaterialCompressService::class;
                break;
            case ExportDownloadModel::FILE_TYPE_APP_MYBANK_WALLET_RECORD:
                $class = ExportMybankWalletRecordService::class;
                break;
            case ExportDownloadModel::FILE_TYPE_WEB_MYBANK_RECORD:
                $class = ExportMybankExpenseRecordListService::class;
                break;
            case ExportDownloadModel::FILE_TYPE_WEB_SHOP_RECORD_LIST:
                $class = ExportShopRecordListService::class;
                break;
            case ExportDownloadModel::FILE_TYPE_WEB_SHOP_STAFF_RECORD_LIST:
                $class = ExportShopStaffRecordListService::class;
                break;
            case ExportDownloadModel::FILE_TYPE_APP_SHOP_PRICE_TAG_LIST:
                $class = ExportPriceTagService::class;
                break;
            default:

                // 导出异常
                $export->dealError();
                SendFs::newRobot(SendFs::ROBOT_WARNING)->setTitleContent('导出对账单错误',
                    "ExportDownloadModel id:{$this->id} 错误的fileType: {$export->fileType}")
                      ->send();

                $this->delete();
                return;
        }

        // 查数据
        $path = $class::getIns($export)->query()->path();

        // 导出完成
        $export->finish($path);

        // 发送飞书通知
        ExportDownloadModel::sendFs($export, $path);

        Log::info('QueueExport success:', ['id' => $this->id]);

        if (config('app.env') == 'dev') {
            $domain = FileUtil::domain();
            dump("QueueExport success: {$this->id}", $domain . "/" . $path);
        }
    }

}
